package com.cz.mes;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.db.Db;
import cn.hutool.db.Entity;

import java.sql.SQLException;
import java.util.List;

public class MrpDel {
    public static void main(String[] args) {
        String dbGroup = "uat3";

        try {
            List<Entity> manufactureOrderList = Db.use(dbGroup).query("select id from manufacture_order where mrp_gen_bom is not null");
            for (Entity manufactureOrder : manufactureOrderList) {
                int orderId = manufactureOrder.getInt("id");

                List<Entity> manufactureOrderDetailList = Db.use(dbGroup).query("SELECT * from manufacture_order_detail where main_id =?", orderId);
                if (CollUtil.isNotEmpty(manufactureOrderDetailList)) {
                    for (Entity manufactureOrderDetail : manufactureOrderDetailList) {
                        int detailId = manufactureOrderDetail.getInt("id");
                        List<Entity> productionMaterialList = Db.use(dbGroup).query("select * from production_material where moc_detail_id =?", detailId);
                        if(CollUtil.isNotEmpty(productionMaterialList)){
                            for (Entity productionMaterial : productionMaterialList) {
                                int materialId = productionMaterial.getInt("id");
                                Db.use(dbGroup).execute("delete from production_material_detail where production_material_id =?", materialId);
                                Db.use(dbGroup).execute("delete from production_material where id= ?",materialId);
                            }
                        }
                        Db.use(dbGroup).execute("delete from manufacture_order_detail where id =?", detailId);
                    }
                }
                Db.use(dbGroup).execute("delete from manufacture_order where id =?", orderId);
            }
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

    }
}
